package com.example.jdai.jdai.threeparty.zm;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.android.moblie.zmxy.antgroup.creditsdk.app.CreditApp;
import com.android.moblie.zmxy.antgroup.creditsdk.app.ICreditListener;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by befairyliu on 16/1/30.
 */
public class DemoPresenterImpl implements DemoPresenter {

    //private static final String TAG = "ZHIMA_DemoPresenterImpl";
    //private DemoView demoView;
    private Activity activity;
    TipDone tipDone;
    String par;
    String sign;

    public DemoPresenterImpl(Activity activity,String par,String sign) {
        this.activity = activity;
        this.par=par;
        this.sign=sign;
        //this.demoView = demoView;

    }

    @Override
    public void doCreditRequest() {
        //测试数据，此部分数据，请由商户服务端生成下发，具体见开放平台商户对接文档
        //请注意params、sign为encode过后的数据
        //String params = "WxjLaHFEe5I7fgerubk86WfkeTNlOUFeLdj1VEaCx4dics06df8nBDXx758%2BiC%2FNaCx1RcVwY%2B96MDexdn0nwMdWVraQtdQ2uJMp1UoAJQviaPB5nUY4XZsKzsx9zBRudl6bfkCoZ7MnMcDo%2FVZsXKcK%2FsbytFbSIc30SkwDYd1lZezmw6g3wQhleLKi%2BObKy9j1InpTLeWj5elkAJ6bJArcjcUvkof8E6LSuvGlRMcgHR40McJVLcVg%2BuDfGmIVSm8pLvQnt9pSqj0jXovfeqXE%2Flnmk5%2BHsZTPq2QlwG8vbw2qjxVtUadNdgNACc5ib4TJfDN%2F2S9%2F784FfseMwQ%3D%3D";
        String appId = "1001695";
        //String sign = "Ei2o%2BC0F2CeAJj0P4%2FBOFhDcn%2F6Ownv8ubyPG3eBddTGComa6YPVBR%2FRYalOuUOxf3r3WQQ7jO8xcBmOxq%2Bljm%2BEz6CStIAQg6GeLu63ME2bpc9H4hPyPm18h1lghGxuuQuKV4PJaxdvSumvj8HXnBf351TyQyzm1U39BdeEY7Y%3D";
        //extParams参数可以放置一些额外的参数，例如当biz_params参数忘记组织auth_code参数时，可以通过extParams参数带入auth_code。
        //不过建议auth_code参数组织到biz_params里面进行加密加签。
        Map<String, String> extParams = new HashMap<>();
        //extParams.put("auth_code", "M_FACE");

        try {
            //请求授权
            CreditAuthHelper.creditAuth(activity, appId, par, sign, extParams, new ICreditListener() {
                @Override
                public void onComplete(Bundle result) {
                    //toast message
                    if (tipDone!=null){
                        tipDone.ok(result.getString("open_id"));

                    }
                    //从result中获取params参数,然后解析params数据,可以获取open_id。
                    if (result != null) {
                        Set<String> keys = result.keySet();
                        for (String key : keys) {
                            Log.d("key", key + " = " + result.getString(key));
                        }
                    }
                }

                @Override
                public void onError(Bundle result) {
                    //toast message
                    if (tipDone!=null){
                        tipDone.error();
                        CreditApp.destroy();

                    }
                    Log.v("erre","pyj");
                }

                @Override
                public void onCancel() {
                    //toast message
                    if (tipDone!=null){
                        tipDone.cancle();
                        CreditApp.destroy();

                    }
                    Log.v("erre","pyjd");
                }
            });
        } catch (Exception e) {
            Log.e("demop", "DemoPresenterImpl.doCreditAuthRequest.exception=" + e.toString());
        }
    }
    public interface TipDone{
        void ok(String openid);
        void cancle();
        void error();
    }
    public void setTipDone(TipDone tipDone){
        this.tipDone=tipDone;
    }
}
